package com.anysoft.web;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;

import com.anysoft.MentorConst;
import com.anysoft.R;
import com.anysoft.WebViewNetworkProtocol;
import com.anysoft.event.TimeOutEvent;
import com.anysoft.me.base.BaseWebViewActivity;
import com.anysoft.utils.ActivityUtils;
import com.anysoft.utils.CommonUtil;
import com.anysoft.utils.HttpUtils;
import com.anysoft.utils.MentorLogger;
import com.anysoft.utils.StringUtils;
import com.anysoft.view.picker.DatePickerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Calendar;
import java.util.Date;

import butterknife.OnClick;

/**
 * 理财日历
 * Created by wyb on 2017/3/26.
 */

public class CalendarFinanceActivity extends BaseWebViewActivity {

    private String token = "";
    private String year;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isShowTitleBar(true);
        year = StringUtils.formatDate(new Date(), "yyyy");
        title.setText(year + "年");
        Drawable drawable = getResources().getDrawable(R.drawable.ic_bank_select_down);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        title.setCompoundDrawables(null, null, drawable, null);
        token = getToken();
        rightButton.setVisibility(View.INVISIBLE);
        generateUrl("calendarFinance", HttpUtils.getCommonParams2StringByWeb(mActivity) + "&year=" + year
                + "&shareClass=A&cusNo=" + getFundCustomerNo());
        if (CommonUtil.isNetWorkConnected(this)) {
            setWebViewConfig(loadUrl);
        }
    }


    @Override
    protected void onPause() {
        super.onPause();
    }


    private void initData() {
        generateUrl("calendarFinance", HttpUtils.getCommonParams2StringByWeb(mActivity) + "&year=" + year
                + "&shareClass=A&cusNo=" + getFundCustomerNo());
        if (CommonUtil.isNetWorkConnected(mActivity)) {
            reLoadPage(loadUrl);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!token.equals(getToken())) {
            token = getToken();
        }
    }

    public void onshouldOverrideUrlLoadingCallBack(WebView webview, String url) {
        MentorLogger.Log(TAG, "url=" + url);
        if (url.contains("prompt.html")) {
            mIntent.setClass(mActivity, SelectWarmPromptActivity.class);
            mIntent.putExtra("url", url);
            startActivity(mIntent);
        } else if (url.contains(WebViewNetworkProtocol.GOTO_LOGIN)) {
            ActivityUtils.gotoLogin(mActivity, hasUser(), false, findUserId());
            EventBus.getDefault().post(new TimeOutEvent(true));
        } else if (url.contains(WebViewNetworkProtocol.GO_QUESTION)) {
            startRiskActivity(null);
        } else {
            webview.loadUrl(url);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (datePickerView != null && datePickerView.isShowing()) {
                datePickerView.dismiss();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    private DatePickerView datePickerView;

    /**
     * 选择年份
     */
    private void selectCertificatesValidity() {
        //时间选择器
        datePickerView = new DatePickerView(mActivity, MentorConst.DateType.YEAR);
        datePickerView.setTitle("请选择您的查询年份");
        datePickerView.setTitle_Str("请选择您的查询年份");
        //控制时间范围
        Date date = new Date();//取时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.DATE, 1);//1往后一天，-1提前一天
//                Calendar calendar = Calendar.getInstance();
        //要在setTime 之前才有效果哦
        System.out.println(calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + (calendar.get(Calendar.DAY_OF_MONTH)));
        datePickerView.setCurrentTime(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1,
                calendar.get(Calendar.DAY_OF_MONTH));
        datePickerView.setRange(1900, Integer.parseInt(StringUtils.formatDate(new Date(), "yyyy")));
        datePickerView.setTime(new Date());
        datePickerView.setCyclic(false);
        datePickerView.setCancelable(true);
        //时间选择后回调
        datePickerView.setOnTimeSelectListener(new DatePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date) {
                year = StringUtils.formatDate(date, "yyyy");

                title.setText(year + "年");
                initData();
            }
        });
        datePickerView.show();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTimeOutCallBack(TimeOutEvent event) {
        if (event.isSuccess) {
            finish();
        }
    }

    @OnClick({R.id.leftButton, R.id.rightButton, R.id.title})
    public void onClick(View view) {
        if (!isFastDoubleClick()) {
            collapseSoftInputMethod(view.getWindowToken());
            switch (view.getId()) {
                case R.id.leftButton:
                    finish();
                    break;
                case R.id.title:
                    selectCertificatesValidity();
                    break;
                case R.id.rightButton:
                    mIntent.setClass(mActivity, CalendarHistoryActivity.class);
                    startActivity(mIntent);
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
